package org.jetpac.chatscript.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;

public class MainPanel extends Composite {
	
	private final static String APPLICATION_TITLE = "Simple client-side only single-person chat.";
	
	final protected Label title = new Label();
	final protected Label nickNameLabel = new Label();
	final protected TextBox nickNameTextBox = new TextBox();
	final protected TextBox chatString = new TextBox();
	final protected Button sendButton = new Button();
	final protected Grid mainGrid = new Grid(3, 2);
	final protected HorizontalPanel entryPanel = new HorizontalPanel();

	final protected ChatPanel chatPanel = new ChatPanel();
	final protected MenuPanel menuPanel = new MenuPanel(this);


	public MainPanel() {
		
		// view 
		title.setText(APPLICATION_TITLE);
		nickNameLabel.setText("Nick:");
		nickNameTextBox.setVisibleLength(15);
		chatString.setText("");
		chatString.setWidth("100%");
		sendButton.setText("Send");
		sendButton.setEnabled(false);

		entryPanel.add(nickNameLabel);
		entryPanel.add(nickNameTextBox);
		entryPanel.add(chatString);
		entryPanel.add(sendButton);
		entryPanel.setCellWidth(chatString, "85%");

		mainGrid.setWidth("100%");
		mainGrid.setWidget(0, 0, title);
		mainGrid.setWidget(1, 0, chatPanel);
		mainGrid.setWidget(2, 0, entryPanel);
		mainGrid.setWidget(1, 1, menuPanel);

		mainGrid.setBorderWidth(1);
		mainGrid.setCellPadding(0);
		mainGrid.setCellSpacing(0);
		
		mainGrid.getCellFormatter().setHorizontalAlignment(0, 0,
				HorizontalPanel.ALIGN_CENTER);
		mainGrid.getCellFormatter().setWidth(1, 0, "85%");
		
		mainGrid.getCellFormatter().setHorizontalAlignment(1, 1, HorizontalPanel.ALIGN_CENTER);

		// set grid height
		mainGrid.setHeight(String.valueOf(Window.getClientHeight()));
		Window.addResizeHandler(new ResizeHandler() {
			public void onResize(ResizeEvent event) {
				mainGrid.setHeight(String.valueOf(event.getHeight()));
				chatPanel.setRelativeHeight();
			}
		});

		
		// LOGIC
		
		// Enable/disable send button
		nickNameTextBox.addValueChangeHandler(new ValueChangeHandler<String>() {
			public void onValueChange(ValueChangeEvent<String> event) {
				System.out.println(event.getValue());
				if (event.getValue().equals(""))
					sendButton.setEnabled(false);
				else
					sendButton.setEnabled(true);

			}
		});

		// when the nickName is entered, move to chatstring
		nickNameTextBox.addKeyPressHandler(new KeyPressHandler() {
			public void onKeyPress(KeyPressEvent event) {
				if (event.getCharCode() == KeyCodes.KEY_ENTER) {
					DeferredCommand.addCommand(new Command() {
						public void execute() {
							chatString.setFocus(true);
						}
					});
				}
			}
		});

		// once ENTER is pressed in the enterChatString module, hit the button
		chatString.addKeyPressHandler(new KeyPressHandler() {

			public void onKeyPress(KeyPressEvent event) {
				if (event.getCharCode() == KeyCodes.KEY_ENTER) {
					DeferredCommand.addCommand(new Command() {
						public void execute() {
							sendButton.click();
						}
					});
				}
			}
		});

		sendButton.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				addChatString(chatString.getText());
			}
		});

		initWidget(mainGrid);

		DeferredCommand.addCommand(new Command() {
			public void execute() {
				nickNameTextBox.setFocus(true);
				Window.setTitle(APPLICATION_TITLE);
			}
		});
	}

	public void addChatString(String text) {
		String stringToAdd;
		stringToAdd = "<" + nickNameTextBox.getText() + "> " + text;

		chatPanel.addString(stringToAdd);

		chatString.setText("");
		chatString.setFocus(true);
	}
}
